package com.example.testproject.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.forEach
import androidx.databinding.DataBindingUtil
import com.airbnb.lottie.LottieCompositionFactory
import com.airbnb.lottie.LottieDrawable
import com.example.testproject.R
import com.example.testproject.databinding.LottieDataBinDing
import com.google.android.material.bottomnavigation.BottomNavigationItemView
import com.google.android.material.bottomnavigation.BottomNavigationView

/**
 *
 * @ClassName: LottieActivity
 * @Author: android 超级兵
 * @CreateDate: 1/24/22$ 4:16 PM$
 * TODO Lottie动画
 * 参考: https://juejin.cn/post/6867895624025997320#heading-5
 * lottie下载地址: https://www.pixeltrue.com/free-packs/error-state#preview
 */
class LottieActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener,
    BottomNavigationView.OnNavigationItemReselectedListener {
    private val mNavigationAnimationList = arrayListOf(
        LottieAnimation.HOME,
        LottieAnimation.SUBSCRIBE,
        LottieAnimation.DISCOVERY,
        LottieAnimation.ACCOUNT
    )

    private lateinit var dataBinDing: LottieDataBinDing

    @SuppressLint("WrongThread")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dataBinDing =
            DataBindingUtil.setContentView(this, R.layout.activity_lottie)
//
//        LottieCompositionFactory.fromAssetSync(
//            dataBinDing.bottomNavigation.context.applicationContext, animation.value
//        )


        initBottomNavigationView()
    }

    private fun initBottomNavigationView() {
        dataBinDing.bottomNavigation.menu.apply {
            for (i in 0 until mNavigationAnimationList.size) {
                add(Menu.NONE, i, Menu.NONE, "${mNavigationAnimationList[i]}")
            }
            setLottieDrawable(mNavigationAnimationList)
        }
        initEvent()
    }

    private fun initEvent() {
        dataBinDing.bottomNavigation.setOnNavigationItemSelectedListener(this)
        dataBinDing.bottomNavigation.setOnNavigationItemReselectedListener(this)
        // 默认选中第一个
        dataBinDing.bottomNavigation.selectedItemId = 0
        // 处理长按 MenuItem 提示 TooltipText
        dataBinDing.bottomNavigation.menu.forEach {
            val menuItemView = findViewById<BottomNavigationItemView>(it.itemId)
            menuItemView.setOnLongClickListener {
                true
            }
        }
    }

    private fun Menu.setLottieDrawable(lottieAnimationList: ArrayList<LottieAnimation>) {
        for (i in 0 until mNavigationAnimationList.size) {
            findItem(i)?.icon =
                getLottieDrawable(lottieAnimationList[i], dataBinDing.bottomNavigation)
        }
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        handleNavigationItem(item)
        return true
    }

    override fun onNavigationItemReselected(item: MenuItem) {
        handleNavigationItem(item)
    }

    private var mPreClickPosition = 0
    private fun handleNavigationItem(item: MenuItem) {
        handlePlayLottieAnimation(item)
        mPreClickPosition = item.itemId
    }

    private fun handlePlayLottieAnimation(item: MenuItem) {
        val currentIcon = item.icon as? LottieDrawable
        currentIcon?.apply {
            playAnimation()
        }
        // 处理 tab 切换，icon 对应调整
        if (item.itemId != mPreClickPosition) {
            dataBinDing.bottomNavigation.menu.findItem(mPreClickPosition).icon =
                getLottieDrawable(
                    mNavigationAnimationList[mPreClickPosition],
                    dataBinDing.bottomNavigation
                )
        }
    }

    /**
     * 获取 Lottie Drawable
     */
    private fun getLottieDrawable(
        animation: LottieAnimation,
        bottomNavigationView: BottomNavigationView
    ): LottieDrawable {
        return LottieDrawable().apply {
            val result = LottieCompositionFactory.fromAssetSync(
                bottomNavigationView.context.applicationContext, animation.value
            )
            callback = bottomNavigationView
            composition = result.value
        }
    }
}

enum class LottieAnimation(val value: String) {
    HOME("tissue.json"),
    SUBSCRIBE("chemical.json"),
    DISCOVERY("cow.json"),
    ACCOUNT("astronaout.json"),
}
